package com.heima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.junit.Test;

import java.util.Arrays;

public class AOPAdvice {
    public void before(JoinPoint joinPoint){
        System.out.println("执行业务方法先执行：before方法");
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println("参数："+ Arrays.toString(args));
        Signature signature = joinPoint.getSignature();
        System.out.println("签名："+signature);
    }
    public void after(){
        System.out.println("执行业务方法后执行：after方法，最终增强");
    }
    public void afterReturning(){
        System.out.println("执行业务方法后执行：afterReturning方法，后置增强");
    }
    public void afterThrowing(Exception e){
        System.out.println("执行业务方法出现异常执行：afterThrowing方法");
        //输出异常类型
        System.out.println("异常类型："+e.getMessage());
    }

    //只有ProceedingJoinPoint才能执行原始方法 并获取方法返回值
    public Object around(ProceedingJoinPoint joinPoint){
        try {
            System.out.println("前置增强");
            //调用原始方法
            Object result = joinPoint.proceed();
            System.out.println("后置增强");
            return result;
        }catch (Throwable throwable) {
            System.out.println("异常增强");
            throwable.printStackTrace();
        }finally {
            System.out.println("最终增强");
        }
        return null;
    }

}
